package com.youxin.logcenter;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

import org.apache.log4j.BasicConfigurator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.youxin.busfoundation.config.KafkaConfig;
import com.youxin.busfoundation.log.kafka.KafkaLevel;
import com.youxin.foundation.utils.thread.BaseThreadPool;
import com.youxin.foundation.utils.thread.ScheduleThreadPool;
import com.youxin.logcenter.kafka.KafkaConsumerLog;

/**
 * 启动入口
 * 
 * @author yingde.cao
 *
 */

@SpringBootApplication
@EnableEurekaClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
@EnableZuulProxy
@EnableConfigurationProperties({KafkaConfig.class})
//@RibbonClient(name = "logcenter",configuration = RibbonAutoConfiguration.class)
@Configuration
public class LogCenterApplication {
	@Autowired 
	private KafkaConsumerLog kafkaConsumerLog;
	@Autowired
	private KafkaConfig kafkaConfig;
	@Autowired
	private Environment environment;
	
	@Value("${eureka.client.serviceUrl.defaultZone}")
	private String eurekaServer;
	@Value("${spring.datasource.url}")
	private String dataSourceUrl;
	
	
//	@Autowired
//	private DataSource dataSource;
//	
//	@Autowired
//	private JdbcTemplate jdbcTemplate;
	
	private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(LogCenterApplication.class);
	public static void main(String[] args) {
		// BasicConfigurator.configure(); //防止出现log4j: WARN no
		// appender警告，也可配制解决。
		BasicConfigurator.configure();
		SpringApplication.run(LogCenterApplication.class, args);
	}

	@Autowired
	private Environment env;

//	@Value("${server.port}")
//	String port;
//		
//	@RequestMapping("/aaa")
//	@HystrixCommand(fallbackMethod = "hiError")
//	public String testaa() {
//		return "ok";
//	}
//
//	
//
//	@RequestMapping("/hi")
//	@HystrixCommand(fallbackMethod = "hiError")
//	public String home(@RequestParam String name) {
//
//		String address = env.getProperty("spring.rabbitmq.addresses", "");
//		return "hi " + name + ",i am from port:" + port;
//	}
//
//	@RequestMapping("/hi/aa")
//	@HystrixCommand(fallbackMethod = "hiError")
//	public String home2(@RequestParam String name) {
//		String address = env.getProperty("spring.rabbitmq.addresses", "");
//		return "hi " + name + ",i am from port:" + port;
//	}
//
//	@RequestMapping(value = "/post", method = RequestMethod.POST)
//	public String postTest(@RequestParam String name) {
//		return "hi " + name + ",i am from port:" + port;
//	}
//
//
//	public String hiError(Throwable e) {
//		throw new RuntimeException("fallback1 failed");
//
//		// return "hi,Error!";
//	}
//
//	public String hiError(String name) {
//		return "hi," + name + ",sorry,error!";
//	}

	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}

	/**
	 * 打印当前
	 */
	private void getActiveProfiles() {
		logger.warn("******加载配置,dbURL:"+dataSourceUrl+",EurekaServer:"+eurekaServer);
		for (final String profileName : environment.getActiveProfiles()) {
			logger.warn("******当前使用的配置 - " + profileName);
		}
	}

	@PostConstruct
	private void init() {		
		try {		
			getActiveProfiles();
			KafkaConfig.initConfig(kafkaConfig);			
			ScheduleThreadPool.instance(6);
			BaseThreadPool.instance(10,Integer.MAX_VALUE, 10000);
			kafkaConsumerLog.execute();

		} catch (Exception e) {
			logger.error("加载异常", e);	
		}	
	}

	// http://localhost:8762/hystrix.stream
	// http://localhost:8762/hystrix/
}
